#include "unp.h"
#include "client.h"

#define MAX 5

int baserand(int x, int y) {	//random area assign
	static int z = 0;
	int tmp;
	if (z == 0) {
		srand((int) time(NULL));
		rand();
		rand();
		rand();
		rand();
		srand(rand());
		z = 1;
	}

	tmp = rand() % (y - x + 1) + x;
	return tmp;
}

void mixBingo(int **bingo[MAX][MAX]) {

	int i;
	int x1, y1;
	int x2, y2;
	int tmp;

	for (i = 0; i < 10 * MAX; i++) {

		x1 = baserand(0, MAX - 1);
		y1 = baserand(0, MAX - 1);

		x2 = baserand(0, MAX - 1);
		y2 = baserand(0, MAX - 1);

		tmp = bingo[x1][y1];		//change each number
		bingo[x1][y1] = bingo[x2][y2];
		bingo[x2][y2] = tmp;

	}

}

int serverCommand(Client *c, char buf[MAXLINE]) {
	char command[MAXLINE] = { '\0' };
	char id[MAXLINE] = { '\0' };
	char chat[MAXLINE] = { '\0' };
	int cmdNum = 0;

	sscanf(buf, "%s %s %s", command, id, chat);
	if (!strcmp(command, "USER"))
		cmdNum = 1;
	else if (!strcmp(command, "LIST"))
		cmdNum = 2;
	else if (!strcmp(command, "QUIT"))
		cmdNum = 3;
	else if (!strcmp(command, "FROM"))
		cmdNum = 4;
	else if (!strcmp(command, "BINGO"))
		cmdNum = 5;
	else if (!strcmp(command, "MIX"))
		cmdNum = 6;

	return cmdNum;

}

void responseCommand(Client *cli, int cmdNum) {
	char command[MAXLINE] = { '\0' };
	char id[MAXLINE] = { '\0' };
	char talk[MAXLINE] = { '\0' };
	char message[MAXLINE] = { '\0' };
	char idList[MAXLINE] = { '\0' };
	char bingoMatrix[MAXLINE] = { '\0' };
	char str[MAXLINE] = { '\0' };
	int bingoNum = 0;
	int i, j;

	Client *list;

	sscanf(cli->buf, "%s %s %s", command, id, talk);

	if (cmdNum == 1) {	// USER Login
		strcpy(cli->user, id); // insert user ID

		fprintf(stderr, "> %s", cli->buf);
		sprintf(message, "230 User logged in\r\n");
		fprintf(stderr, "< %s", message);

		Writen(cli->connfd, message, strlen(message));

	} else if (cmdNum == 2) {	// LIST
		for (list = getClientList(); list != NULL; list = list->next) {
			strcat(idList, list->user);
			strcat(idList, " ");
		}
		snprintf(message, MAXLINE, "231 Users :%s\r\n", idList);
		fprintf(stderr, "> %s\r\n", command);
		fprintf(stderr, "< %s", message);

		Writen(cli->connfd, message, strlen(message));

		memset(idList, 0x00, strlen(idList));

	} else if (cmdNum == 3) {	// QUIT
		snprintf(message, MAXLINE, "221 service closing");
		fprintf(stderr, "< %s\r\n", command);
		fprintf(stderr, "< %s", message);
		Writen(cli->connfd, message, strlen(message));

	} else if (cmdNum == 4) { 	// FROM (Chat)
		if (cli->user[0] != '\0') {
			snprintf(message, MAXLINE, "299 %s", cli->buf);
			for (list = getClientList(); list != '\0'; list = list->next) {
				if ((cli->connfd != list->connfd) && (list->user[0] != '\0')) {
					fprintf(stderr, "< %s", message);
					Writen(list->connfd, message, strlen(message));
				} else { //me
					fprintf(stderr, "< %s", message);
					Writen(list->connfd, message, strlen(message));
					memset(message, 0x00, MAXLINE);
					snprintf(talk, MAXLINE, "200 OK\r\n");
					fprintf(stderr, "< %s", talk);
					Writen(list->connfd, talk, strlen(talk));
				}
			}
		} else {	//remainder
			fprintf(stderr, "> FROM None: %s\r\n", talk);
			snprintf(message, MAXLINE, "530 Not logged in\r\n");
			fprintf(stderr, "< %s", message);
			Writen(cli->connfd, message, strlen(message));
		}
	} else if (cmdNum == 5) { // BINGO <id>: <number>
		if (cli->user[0] != '\0') { // Logged in
			bingoNum = atoi(talk); // this talk is bingo number
			snprintf(message, MAXLINE, "799 %s", cli->buf);
			fprintf(stderr, "< %s", message);
			if (bingoNum > 0 && bingoNum <= MAX * MAX) {
				for (list = getClientList(); list != '\0'; list = list->next) {
					if ((cli->connfd != list->connfd)
							&& (list->user[0] != '\0')) {
						Writen(list->connfd, message, strlen(message));	//if number is in the board
						for (i = 0; i < 5; i++) {
							for (j = 0; j < 5; j++) {
								if (list->bingo[i][j] == bingoNum) {
									list->bingo[i][j] = 0;
								}
							}
						}
						for (i = 0; i < 5; i++) {
							for (j = 0; j < 5; j++) {
								sprintf(str, "%d", list->bingo[i][j]);
								strcat(bingoMatrix, str);
								strcat(bingoMatrix, "\t");
								memset(str, 0x00, MAXLINE);
							}
						strcat(bingoMatrix, "\r\n");
						}
						snprintf(message, MAXLINE, "750 Show bingo\r\n");
						Writen(list->connfd, message, strlen(message));
						Writen(list->connfd, bingoMatrix, strlen(bingoMatrix));
						memset(bingoMatrix, 0x00, MAXLINE);
						memset(message, 0x00, MAXLINE);
						////////////////
					} else { //me
						Writen(list->connfd, message, strlen(message));
						memset(message, 0x00, MAXLINE);
						snprintf(message, MAXLINE, "700 Bingo OK\r\n");
						fprintf(stderr, "< %s", message);
						Writen(list->connfd, message, strlen(message));
						for (i = 0; i < 5; i++) {
							for (j = 0; j < 5; j++) {
								if (cli->bingo[i][j] == bingoNum) {
									cli->bingo[i][j] = 0;
								}
							}
						}
						for (i = 0; i < 5; i++) {
							for (j = 0; j < 5; j++) {
								sprintf(str, "%d", list->bingo[i][j]);
								strcat(bingoMatrix, str);
								strcat(bingoMatrix, "\t");
								memset(str, 0x00, MAXLINE);
							}
							strcat(bingoMatrix, "\r\n");
						}
						snprintf(message, MAXLINE, "750 Show bingo\r\n");
						Writen(list->connfd, message, strlen(message));
						Writen(list->connfd, bingoMatrix, strlen(bingoMatrix));
						memset(bingoMatrix, 0x00, MAXLINE);
						memset(message, 0x00, MAXLINE);
						///////////////////////
					}
				}
			} else if (bingoNum <= 0 || bingoNum > MAX * MAX) {
				snprintf(message, MAXLINE, "720 Wrong number\r\n");
				fprintf(stderr, "< %s", message);
				Writen(cli->connfd, message, strlen(message));
				/////////////////////////////
			}

		} else { //Not logged in
			fprintf(stderr, "> BINGO None: %s\r\n", talk);
			snprintf(message, MAXLINE, "530 Not logged in\r\n");
			fprintf(stderr, "< %s", message);
			Writen(cli->connfd, message, strlen(message));
		}
	} else if (cmdNum == 6) { // MIX
		if (cli->user[0] != '\0') { // Logged in
			mixBingo(cli->bingo);
			for (i = 0; i < 5; i++) {
				for (j = 0; j < 5; j++) {
					sprintf(str, "%d", cli->bingo[i][j]);
					strcat(bingoMatrix, str);
					strcat(bingoMatrix, "\t");
					memset(str, 0x00, MAXLINE);
				}
				strcat(bingoMatrix, "\n");
			}
			strcat(bingoMatrix, "\n");
			snprintf(message, MAXLINE, "800 Mix OK\r\n");
			fprintf(stderr, "< %s", message);
			Writen(cli->connfd, message, strlen(message));
			snprintf(message, MAXLINE, "%s", bingoMatrix);
			Writen(cli->connfd, message, strlen(message));

		} else { //Not logged in
			fprintf(stderr, "> MIX\r\n");
			snprintf(message, MAXLINE, "530 Not logged in\r\n");
			fprintf(stderr, "< %s", message);
			Writen(cli->connfd, message, strlen(message));
		}
	} else
		fprintf(stderr, "500 syntax error");

	memset(command, 0x00, MAXLINE);
	memset(id, 0x00, MAXLINE);
	memset(talk, 0x00, MAXLINE);
	memset(message, 0x00, MAXLINE);
	memset(cli->buf, 0x00, MAXLINE);
	bingoNum = 0;

}

